package com.relax.mybatis.plugin.sensitive.config;

import javax.annotation.Resource;

import org.springframework.beans.factory.InitializingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Lazy;

import com.relax.mybatis.plugin.sensitive.interceptor.SensitiveReadInterceptor;
import com.relax.mybatis.plugin.sensitive.interceptor.SensitiveWriteInterceptor;
import com.relax.mybatis.plugin.sensitive.strategy.SensitiveContext;
import com.relax.mybatis.plugin.sensitive.strategy.SensitiveFieldHolder;

/**
 * @author relax
 * @date 2022/07/05
 * @desc 脱敏自动配置
 */
@Lazy
@EnableConfigurationProperties({SensitiveProperties.class})
@Configuration(proxyBeanMethods = false)
public class SensitiveAutoConfiguration implements InitializingBean {

    @Resource
    private SensitiveProperties sensitiveProperties;

    @Bean
    public SensitiveReadInterceptor sensitiveReadInterceptor() {
        return new SensitiveReadInterceptor(this.sensitiveProperties);
    }

    @Bean
    public SensitiveWriteInterceptor sensitiveWriteInterceptor() {
        return new SensitiveWriteInterceptor(this.sensitiveProperties);
    }

    @Bean
    @ConditionalOnMissingBean
    public SensitiveContext sensitiveContext() {
        return new SensitiveContext();
    }

    /* (non-Javadoc)
     * @see org.springframework.beans.factory.InitializingBean#afterPropertiesSet()
     */
    @Override
    public void afterPropertiesSet() throws Exception {}

}
